package com.lyhengtongwl.zqsnews.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.base.BaseFragment;
import com.lyhengtongwl.zqsnews.ui.fragment.forward.FavoriteFragment;
import com.lyhengtongwl.zqsnews.ui.fragment.forward.MyUploadFragment;
import com.lyhengtongwl.zqsnews.ui.fragment.forward.SelectedArticlesFragment;
import com.lyhengtongwl.zqsnews.ui.fragment.forward.UploadFragment;
import com.lyhengtongwl.zqsnews.utils.ScreenUtil;

import java.util.ArrayList;

import butterknife.BindView;

public class NewsMMByForwardFragment extends BaseFragment {
    ArrayList<Fragment> mFragments = new ArrayList<>();
    @BindView(R.id.view_status_bar_place)
    View statusBarPlace;
    @BindView(R.id.tab)
    TabLayout tab;
    @BindView(R.id.vp)
    ViewPager vp;

    @Override
    public View setContentView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_newsforward, null);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ViewGroup.LayoutParams params = statusBarPlace.getLayoutParams();
        params.height = ScreenUtil.getStatusBarHeight(getContext());
        statusBarPlace.setLayoutParams(params);
        setTopNavigation();
    }


    private void setTopNavigation() {
        String[] mTitles = {"精选文章", "我的上传", "我的收藏", "上传文章"};
        int[] mIconSelectIds = {
                R.drawable.icon_message_selected, R.drawable.icon_contact_selected,
                R.drawable.icon_mine_selected, R.drawable.icon_mine_selected};
        //tab的标题、选中图标、未选中图标
        //初始化数据
        mFragments.add(new SelectedArticlesFragment());
        mFragments.add(new MyUploadFragment());
        mFragments.add(new FavoriteFragment());
        mFragments.add(new UploadFragment());
        vp.setOffscreenPageLimit(mTitles.length-1);
        vp.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return mFragments.get(i);
            }

            @Override
            public int getCount() {
                return mTitles.length;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return mTitles[position];
            }
        });
        tab.setupWithViewPager(vp);
        tab.removeAllTabs();
        //给tab设置数据和关联的fragment
        for (int i = 0; i < mTitles.length; i++) {
            tab.addTab(tab.newTab().setText(mTitles[i]).setIcon(mIconSelectIds[i]));
        }
    }
}
